// created by noellynch
// May 8, 2009

package com.sceneworker.monitorpopup;

import java.awt.event.ActionEvent;

import javax.swing.JDialog;
import javax.swing.tree.TreePath;

import com.jmex.terrain.TerrainPage;
import com.myjavautils.swing.SimpleAlert;
import com.sceneworker.app.globals.SceneWorkerDialogManager;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.app.globals.cacheobjects.ISceneWorkerCacheObject;
import com.sceneworker.app.globals.cacheobjects.TerrainAndHeightMapCacheObject;
import com.sceneworker.editors.proceduraltexturegeneratoreditor.ProcTextureGenPanel;

public class TerrainPageMenu extends BaseMenu {

	private static final String PROCEDURAL_TEXTURE_GENERATOR = "Procedural Texture Generator...";
	private		TerrainPage			mcl_terrain;
	
	public TerrainPageMenu(TreePath cl_selectedPath, SceneWorkerPopUp cl_menu) {
		super(cl_menu);
		
		mcl_terrain = (TerrainPage)cl_selectedPath.getLastPathComponent();
		
		buildMenu();
	}

	@Override
	public void actionPerformed(ActionEvent cl_e) {
		if(cl_e.getActionCommand().equals(PROCEDURAL_TEXTURE_GENERATOR)) {
			// get the object from the cache
			ISceneWorkerCacheObject		lcl_obj = SceneWorkerGlobals.inst().getFromCache("obj" + mcl_terrain.hashCode());
			if(lcl_obj == null) {
				SimpleAlert.doAlert("No height map is associated with this terrain page.", mcl_menu);
				return;
			}
			
			if(lcl_obj instanceof TerrainAndHeightMapCacheObject) {
				JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.PROCEDURAL_TEXTURE_GENERATION_DIALOG);
				lcl_dlg.add(new ProcTextureGenPanel((TerrainAndHeightMapCacheObject)lcl_obj, lcl_dlg));
				lcl_dlg.setVisible(true);
			} else {
				SimpleAlert.doAlert("No height map is associated with this terrain page.", mcl_menu);
				return;
			}
		}
	}

	@Override
	protected void buildMenu() {
		mcl_menu.addSeparator();
		mcl_menu.add(buildAMenuItem(PROCEDURAL_TEXTURE_GENERATOR));
		mcl_menu.addSeparator();
	}

}
